package com.example.controller;

import com.example.common.ApiRestResponse;
import com.example.common.Constant;
import com.example.exception.ImoocMallException;
import com.example.exception.ImoocMallExceptionEnum;
import com.example.mall.model.request.AddCategory;
import com.example.mall.model.request.UpdateCategory;
import com.example.mall.model.vo.CategoryVO;
import com.example.service.CategoryService;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import com.sun.jndi.toolkit.url.Uri;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.UUID;


@RestController
@Api(tags = "ShopClass")
public class Category {
    @Autowired
    CategoryService categoryService;
    UserService userService;
    //添加商品分类
    @ApiOperation(value = "添加商品分类")
    @PostMapping("/shop/addProduct")
    @ResponseBody
    public ApiRestResponse addProduct(@Valid @RequestBody AddCategory addCategory) {
        if (addCategory.getName() == null) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NOT_NULL_PRODUCT);
        }
        categoryService.AddProduct(addCategory);
        return ApiRestResponse.success();

    }

    //更新商品分类
    @ApiOperation(value = "更新商品分类")
    @PostMapping("/shop/UpdateCategory")
    public ApiRestResponse UpdateCategory(@Valid @RequestBody UpdateCategory updateCategory) {
        categoryService.UpdateCategory(updateCategory);
        return ApiRestResponse.success();
    }

    //    查看商品分类
    @ApiOperation(value = "管理员分页查询商品分类")
    @PostMapping("/shop/QueryShop")
    public ApiRestResponse QueryShop(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        List<CategoryVO> pageInfo=  categoryService.listForAdmin(pageNum,pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    //    删除商品分类
    @ApiOperation(value = "删除商品分类")
    @PostMapping("/shop/DeleteShopClass")
    public ApiRestResponse DeleteShopClass(@RequestParam Integer id) {
        if (id == null) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.NOT_NULL_ID);
    }
        categoryService.DeleteShopClass(id);
        return ApiRestResponse.success();
    }
    //   图片上传
    @ApiOperation(value = "上传头像")
    @PostMapping("/upload/image")
    public ApiRestResponse uploadImg(HttpServletRequest httpServletRequest, @RequestParam("file")MultipartFile file){
        if (file.isEmpty()) {
            System.out.println("文件为空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(Constant.Upload_File_Dir+fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            return ApiRestResponse.success(getHost(new URI(httpServletRequest.getRequestURL()+""))+"/imgs/"+fileName);
        } catch (URISyntaxException e) {
            return ApiRestResponse.error(ImoocMallExceptionEnum.UPLOAD_IMG);
        }
    }
    //    获取IP、端口号
    private URI getHost(URI uri){
        URI effectiveURI;
        try {
            effectiveURI = new URI(uri.getScheme(),uri.getUserInfo(),uri.getHost(),uri.getPort(),null,null,null);
        } catch (URISyntaxException e) {
            effectiveURI=null;
        }
        return effectiveURI;
    }

}
